/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ci.service.impl;

import com.ci.core.dao.search.Search;
import com.ci.core.dao.search.SearchResult;
import com.ci.dao.port.HDSerialDao;
import com.ci.model.impl.HDSerial;
import com.ci.service.port.HDSerialLocal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 * @author Lang Yu
 * @description
 * @date Aug 31, 2010 10:06:00 AM
 * @version 1.0
 */
@Stateless(name = "HDSerialEJB", mappedName = "ejb/HDSerialJNDI")
public class HDSerialServiceImpl implements HDSerialLocal{
    @EJB
    private HDSerialDao serialDao;
    
    @Override
    public HDSerial create(HDSerial serial) {
        this.getSerialDao().persist(serial);
        return serial;
    }

    @Override
    public Map<Integer,List<HDSerial>> getAll(String sortField,boolean sortOrder,Map<String,String> filter,int pageIndex, int pageSize) {
        Search search = new Search(HDSerial.class);
        search.addSort(sortField, sortOrder);
        for( Entry<String,String> entry: filter.entrySet() ){
            search.addFilterILike(entry.getKey(), entry.getValue());
        }
        search.setPage(pageIndex);
        search.setMaxResults(pageSize);
        search.setFirstResult(pageIndex * pageSize);
        SearchResult<HDSerial> result = getSerialDao().searchAndCount(search);
        Map<Integer,List<HDSerial>> resultMap = new HashMap<Integer,List<HDSerial>>();
        resultMap.put(result.getTotalCount(), result.getResult());
        return resultMap;
    }

    /**
     * @return the serialDao
     */
    public HDSerialDao getSerialDao() {
        return serialDao;
    }

    @Override
    public List<HDSerial> getAll() {
        Search search = new Search(HDSerial.class);
        search.addSort("id", true);
        SearchResult<HDSerial> result = getSerialDao().searchAndCount(search);
        return result.getResult();
    }

}
